package com.sxz.sbs.module.mine;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.dialog.loading.LoadingDialog;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.module.mine.mode.GeneralizeCodeBean;
import com.sxz.sbs.module.mine.presenter.GeneralizeCodePresenter;
import com.sxz.sbs.module.mine.view.GeneralizeCodeView;
import com.sxz.sbs.widget.CommonTitle;
import java.io.UnsupportedEncodingException;
import butterknife.BindView;

/**
 * @author hp
 * @date 2018/3/22
 * 我的推广码
 */

public class GeneralizeCodeActivity extends BaseActivity<GeneralizeCodePresenter> implements GeneralizeCodeView {

    /**
     * 能不能提交
     */
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.webview_generalize)
    WebView webviewGeneralize;
    private int id;
    private String name, phone;
    private LoadingDialog dialog;

    @Override
    protected void init() {
        new CommonTitle.Builder(this).showMore(false).setTitle("我的推广码").showRightText(true).build().show();
        dialog= LoadingDialog.create(this).setCancellable(true).setDimAmount(0.5f);
        tvTitleRight.setText("切换");
        /**
         * 获取id name phone
         */
        id = Integer.valueOf(getIntent().getStringExtra("personageId"));
        name = getIntent().getStringExtra("unicodeName");
        phone = getIntent().getStringExtra("personagePhone");
        /**
         * 设置与Js交互的权限
         * 允许JavaScript执行
         */
        webviewGeneralize.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webviewGeneralize.getSettings().setJavaScriptEnabled(true);
        webviewGeneralize.getSettings().setLoadWithOverviewMode(true);
        /**
         * 加载网页的监听
         */
        webviewGeneralize.setWebViewClient(new WebViewClient(){
            // 重写shouldOverrideUrlLoading方法，使点击链接后不使用其他的浏览器打开。
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                // 如果不需要其他对点击链接事件的处理返回true，否则返回false
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
                //打开时显示加载框
                dialog.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                //加载完成关闭加载框
                dialog.dismiss();
            }
        });
        //找到Html文件，也可以用网络上的文件
//        LogUtils.i("http://120.27.27.144:8088/ma1/?id=" + id + "&name=" + name + "&phone=" + phone);
        webviewGeneralize.loadUrl("http://120.27.27.144:8088/ma1/?id=" + id + "&name=" + name + "&phone=" + phone);

        tvTitleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 通过Handler发送消息
                webviewGeneralize.post(new Runnable() {
                    @Override
                    public void run() {
                        // 注意调用的JS方法名要对应上
                        // 调用javascript的changeSkin()方法
                        webviewGeneralize.loadUrl("javascript:changeSkin()");
                    }
                });
            }
        });

    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_generalizecode;
    }

    @Override
    protected GeneralizeCodePresenter createPresenter() {
        return null;
    }

    @Override
    public void onGeneralizeCodeSuccess(GeneralizeCodeBean bean) {

    }
}
